{% extends 'envs_python/layout_envs_python.html' %}
{% load foundation_tags %}

{% block html_js_code %}
{{ block.super }}
<script>
layui.use(function(){
  var $ = layui.$;
  var layer = layui.layer;
  var util = layui.util;

function parseXHRError(xhr) {
    let message = '未知错误';

    try {
        const response = JSON.parse(xhr.responseText);
        if (response && response.message) {
            message = response.message;
        } else if (xhr.statusText) {
            message = xhr.statusText;
        }
    } catch (e) {
        if (xhr.statusText) {
            message = xhr.statusText;
        }
        console.error('无法解析错误响应:', xhr.responseText);
    }

    return message;
    }
    util.on('lay-on', {
        'startinstall': function (){
        var versionName = $(this).attr('data');
        var loadIndex = layer.msg('下载Python安装包...', {
            icon: 16,
            shade: 0.6,
            time: 0,
        });

        $.ajax({
            url: '{% url  'envs_python_runtime:download' %}',
            type: 'GET',
            data: {
                version: versionName,
            },
            success: function (response) {
                layer.close(loadIndex);
                layer.msg('下载成功~开始安装....', {icon: 16, time:0, shade: 0.6});

                $.ajax({
                    url: '{% url  'envs_python_runtime:install' %}',
                    type: 'get',
                    data: {
                        version: versionName
                    },
                    success: function (response) {
                        if (response.status == 'success') {
                            location.href = '{% url 'envs_python_runtime:python_list' %}'
                        }
                    },
                    error: function (xhr, status, error) {
                        let errorMsg = '安装失败: ';
                        const message = parseXHRError(xhr);
                        layer.msg(errorMsg + message, {icon:2});
                    }
                 })

            },
            error: function (xhr, status, error) {
                let errorMsg = '下载失败: ';
                const message = parseXHRError(xhr);
                layer.msg(errorMsg + message, {icon:2});
            }
        });
    }
  })
});
</script>
{% endblock %}

{% block page_content %}
    {{ block.super }}
{% include 'include_messages.html' %}
<div class="card">
    <div class="card-header">
        <div class="card-tools">
           <a href="javascript:location.reload();" class="btn btn-outline-info">
                <i class="fas fa-sync"></i> 刷新
            </a>
            <a href="{% url 'envs_python_runtime:versions_refresh' %}" class="btn btn-outline-info">
                <i class="fas fa-list"></i> 更新版本
            </a>
        </div>
    </div>
    <div class="card-body p-2">
<div>以下已安装版本仅根据安装记录显示，并未验证真实性。。</div>
        <table class="table table-hover table-striped">
            <thead>
                <tr>
                    <th>名称</th>
                    <th>版本</th>
                    <th>标签</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
{% for name, version in versions.items %}
                <tr{% if name in installed %} class="table-success" {% endif %}>
                    <td>{{ name }}</td>
                    <td>{{ version.sort_version }}</td>
                    <td>{{ version.tag }}</td>
                    <td>
    <a href="{% url 'envs_python_runtime:python_install' name %}" data="{{ name }}" class="btn btn-primary btn-xs">安装</a>
{% if version.file_name|slice:"-4:" == ".zip" %}
    <a href="{% replace_str version.url 'https://www.python.org/ftp/python/' download_url_prefix %}" class="btn btn-outline-info btn-xs">另存下载</a>
 {% endif %}
                    </td>
                </tr>
{% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% endblock %}